package com.markus.code.exam.tencent;

import com.markus.basic.Java基础.重写与重载.A;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Author:markusZhang
 * Date:Create in 2020/9/6 20:03
 * todo:
 */
public class Main {
    public static class ListNode{
        int val;
        ListNode next;
        public ListNode(int val){
            this.val = val;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            int n = scanner.nextInt();
            ListNode l1 = new ListNode(scanner.nextInt());
            ListNode node = l1;
            for (int i = 1;i<n;i++){
                node.next = new ListNode(scanner.nextInt());
                node = node.next;
            }
            int m = scanner.nextInt();
            ListNode l2 = new ListNode(scanner.nextInt());
            node = l2;
            for (int i=1;i<m;i++){
                node.next = new ListNode(scanner.nextInt());
                node = node.next;
            }
            List<Integer> integers = process(l1, l2);
            for (Integer integer : integers) {
                System.out.print(integer+" ");
            }
        }
    }
    private static List<Integer> process(ListNode l1,ListNode l2){
        List<Integer> list = new ArrayList<>();
        ListNode up = l1;
        ListNode down = l2;
        while(up != null && down != null){
            if (up.val == down.val){
                list.add(up.val);
                up = up.next;
                down = down.next;
            }else if (up.val < down.val){
                down = down.next;
            }else {
                up = up.next;
            }
        }
        return list;
    }
}
